package com.example.wmstore.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wmstore.common.QueryPageParam;
import com.example.wmstore.common.Result;
import com.example.wmstore.entity.Goods;
import com.example.wmstore.entity.Storage;
import com.example.wmstore.entity.User;
import com.example.wmstore.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TCW
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @GetMapping("/allcount")
    public Result allcount(){
        List<Goods> list=goodsService.list();
        int count=0;
        for(Goods goods:list){
            if(goods.getStatus()==1){
                count+=goods.getCount();
            }
        }
        return Result.suc(count);
    }
    @GetMapping("/list")
    public List<Goods> list(){
        return goodsService.list();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods){
        return goodsService.save(goods)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update/{id}")
    public Result update(@RequestBody Goods goods){
        return goodsService.updateById(goods)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return goodsService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String id=(String)param.get("id");
        String name = (String)param.get("name");
        String storage = (String)param.get("storage");

        Page<Goods> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(id) && !"null".equals(id)){
            lambdaQueryWrapper.eq(Goods::getId,id);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Goods::getName,name);
        }
        if(StringUtils.isNotBlank(storage) && !"null".equals(storage)){
            lambdaQueryWrapper.eq(Goods::getStorage,storage);
        }

        IPage result = goodsService.PageList(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

    @PostMapping("/updateLocation")
    public Result updateLocation(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String id=(String)param.get("id");
        String newLocation = (String)param.get("newlocaltion");

        LambdaUpdateWrapper<Goods> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        if (StringUtils.isNotBlank(id) &&!"null".equals(id)) {
            lambdaUpdateWrapper.eq(Goods::getId, id);
            lambdaUpdateWrapper.set(Goods::getGoodslocal, newLocation);
        }

        boolean result = goodsService.update(lambdaUpdateWrapper);
        return Result.suc(result);
    }

    @PostMapping("/updateLocation/{id}")
    public Result updateLocation(@PathVariable("id") Long id, @RequestParam("goodslocal") String newGoodsLocal) {
        Goods goods = goodsService.getById(id);
        if (goods!= null) {
            goods.setGoodslocal(newGoodsLocal);
            return goodsService.updateById(goods)? Result.suc() : Result.fail();
        } else {
            return Result.fail();
        }
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        Goods goods=goodsService.getById(id);
        return Result.suc(goods);
    }
    @GetMapping("/getlocal/{id}")
    public Result getlocal(@PathVariable("id") Long id) {
        Goods goods=goodsService.getById(id);
        if(goods!=null){
            String goodslocal=goods.getGoodslocal();
            return Result.suc(goodslocal);
        }
        else {
            return Result.fail("订单不存在");
        }
    }
}
